package com.bnyy.medicalHousekeeper.moudle.message.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.moudle.message.MessageHeaderHelper;
import com.bnyy.message.bean.Contact;
import com.bnyy.message.bean.ContactGroup;
import com.bnyy.message.bean.ContactUser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/12
 **/
public class SearchContactAdapter extends RecyclerView.Adapter<SearchContactAdapter.ViewHolder> {

    private ArrayList<Object> datas = new ArrayList<>();
    private Context mContext;
    private Consumer<Contact> consumer;
    private LayoutInflater inflater;
    public SearchContactAdapter(Context context, Consumer<Contact> consumer) {
        mContext = context;
        this.consumer = consumer;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getItemViewType(int position) {
        return datas.get(position) instanceof String ? 0 : 1;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void refresh(HashMap<String, ArrayList<? extends Contact>> map) {
        datas.clear();
        if (map != null) {
            map.forEach(new BiConsumer<String, ArrayList<? extends Contact>>() {
                @Override
                public void accept(String s, ArrayList<? extends Contact> contacts) {
                    if (contacts != null && contacts.size() > 0) {
                        datas.add(s);
                        datas.addAll(contacts);
                    }
                }
            });
        }
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new HeaderViewHolder(inflater.inflate(R.layout.item_message_search_contact_header, parent, false));
        } else {
            return new ContactViewHolder(inflater.inflate(R.layout.item_message_contact, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Object o = datas.get(position);
        if (holder instanceof HeaderViewHolder) {
            HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
            headerViewHolder.tvHeader.setText(String.valueOf(o));
        } else {
            ContactViewHolder contactViewHolder = (ContactViewHolder) holder;
            if (o instanceof ContactGroup) {
                ContactGroup contactGroup = (ContactGroup) o;
                contactViewHolder.tvName.setText(contactGroup.getGroup_name());
                MessageHeaderHelper.setHeader(mContext, contactViewHolder.rlHeaders, contactGroup.getImgs());
            } else if (o instanceof ContactUser) {
                ContactUser contactUser = (ContactUser) o;
                contactViewHolder.tvName.setText(contactUser.getUsername());
                MessageHeaderHelper.setHeader(mContext, contactViewHolder.rlHeaders, contactUser.getUser_img());
            }
            contactViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    consumer.accept((Contact) o);
                }
            });
        }
    }

    public static class HeaderViewHolder extends ViewHolder {

        TextView tvHeader;

        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
            tvHeader = itemView.findViewById(R.id.tv_header);
        }
    }

    public static class ContactViewHolder extends ViewHolder {

        RelativeLayout rlHeaders;
        TextView tvName;

        public ContactViewHolder(@NonNull View itemView) {
            super(itemView);

            rlHeaders = itemView.findViewById(R.id.rl_headers);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }

    public static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
